package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.ClientDao;
import po.Client;

@Repository("clientDao")
public class ClientDaoImpl implements ClientDao{

	@PersistenceContext(name="un")
	private EntityManager em;
		
	@Override
	public boolean existClient(String cname) {
		String jpql = "from Client where cname=:cname";
		List<Client> ls = em.createQuery(jpql).setParameter("cname", cname).getResultList();
		if(ls.isEmpty()) 
			return false;
		else 
			return true;
	}

	@Override
	public Client login(String  cname,String cpassword) {
		String jpql = "from Client where cname=:cname and cpassword=:cpassword";
		List<Client> ls = em.createQuery(jpql).setParameter("cname", cname).setParameter("cpassword", cpassword).getResultList();
		if(ls.isEmpty()) 
			return null;
		else return ls.get(0);
	}

	@Override
	public Integer regist(Client c) {
		em.persist(c);
		return 1;
	}

	@Override
	public boolean updateClient(Client c) {

		em.merge(c);

		return true;
	}

}
